import { Injectable } from '@nestjs/common'
import { JwtService } from '@nestjs/jwt'
@Injectable()
export class AuthService {
  constructor(private readonly jwtService: JwtService) {}

  async login(user: any, expiresInDays?: number) {
    const payload = {
      name: user.name,
      userId: user.id
    }
    let signOptions = {}
    if (expiresInDays) {
      signOptions = { expiresIn: expiresInDays * 24 * 60 * 60 } // 转换为秒
    }
    return {
      access_token: await this.jwtService.signAsync(payload, signOptions),
      userId: user.id,
      username: user.name
    }
  }
}
